<template>
  <!-- 面包屑 -->
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>课程中心</el-breadcrumb-item>
      <el-breadcrumb-item><b>本学期课程</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>

  <!-- 数据卡片 -->
  <div class="card-container">
    <el-card
        v-for="course in tableCourse"
        :key="course.cid"
        class="course-card"
        @click="goToCourseDetail(course.cid)"
    >
      <div class="card-content">
        <h3>{{ course.cname }}</h3>
        <p>课程号: {{ course.cid }}</p>
        <p>任课教师: {{ course.tname }}</p>
        <p>上课时间: {{ course.ltime }}</p>
        <p>上课地点: {{ course.location }}</p>
        <p>课时数: {{ course.chour }}</p>
      </div>
      <div class="card-actions">
        <el-button type="primary" @click="goToCourseDetail(course.cid)">进入</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import request from "@/utils/class";
import { useRouter } from 'vue-router';

// 表格信息显示
const tableCourse = ref([]);

const router = useRouter();

onMounted(() => {
  getCourse();
});

const getCourse = () => {
  // 从 sessionStorage 获取 sid
  const loginUser = sessionStorage.getItem('loginUser');
  const user = JSON.parse(loginUser);
  const sid = user.sid;

  request({
    url: '/select/selfcourse',
    method: 'get',
    params: {
      sid: sid
    }
  }).then(req => {
    console.log(req);
    if (req.status === 200) {
      tableCourse.value = req.data.data;
    } else {
      console.error('Failed to fetch course data:', req.data);
    }
  }).catch(error => {
    console.error('Network or other error occurred:', error);
  });
};

// 新增保存 cid 到 sessionStorage 的方法
const saveCidToStorage = (cid) => {
  sessionStorage.setItem('selectedCid', cid);
};

// 点击卡片跳转至课程详情的方法
const goToCourseDetail = (cid) => {
  saveCidToStorage(cid);
  router.push({ path: `/home/classroom/detail` });
};
</script>

<style scoped>
.el-breadcrumb {
  font-size: 18px;
  margin: 20px;
}
.card-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin: 40px 20px 20px 20px;
}

.course-card {
  width: calc(33% - 20px);
  margin: 10px;
  cursor: pointer;
  transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.card-content {
  padding: 20px;
}

.card-actions {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

@media (max-width: 1200px) {
  .course-card {
    width: calc(50% - 20px);
  }
}

@media (max-width: 600px) {
  .course-card {
    width: 100%;
  }
}
</style>